package com.xique.blelock.Adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.text.TextUtils;
import android.util.TimeUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.clj.fastble.data.BleDevice;
import com.qmuiteam.qmui.widget.QMUILoadingView;
import com.xique.MyApplication;
import com.xique.blelock.Interface.OnItemClickListener;
import com.xique.blelock.R;
import com.xique.blelock.Utils.TimeUtil;
import com.xique.blelock.model.OpenResult;
import com.xique.blelock.model.Operationinfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/12/7.
 */

public class RecordAdapter extends Adapter<RecordAdapter.ViewHolder> {
    private static final int TYPE_FOOT = 1002;
    private static final int TYPE_NORMAL =  1003;
    private List<Operationinfo> mDatas = new ArrayList<>();
    private OnItemClickListener onItemClickListener;

    public RecordAdapter(List<Operationinfo> bleDevices) {
        this.mDatas.addAll(bleDevices);
    }

    public void setDeviceList(List<Operationinfo> deviceList){
        this.mDatas.clear();
        this.mDatas.addAll(deviceList);
    }

    @NonNull
    @Override
    public RecordAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //LayoutInflater.from指定写法

                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_record, parent, false);
                ViewHolder viewHolder = new ViewHolder(view);
                viewHolder.rootlayout = view.findViewById(R.id.rootlayout);
                viewHolder.name = view.findViewById(R.id.username);
                viewHolder.time = view.findViewById(R.id.mdate);
                viewHolder.rightbtn = view.findViewById(R.id.imagebtn);
                return viewHolder;


    }
    @Override
    public int getItemViewType(int position) {
        if (position>=mDatas.size()){
            return TYPE_FOOT;
        }
        return TYPE_NORMAL;
    }


    @Override
    public void onBindViewHolder(@NonNull final RecordAdapter.ViewHolder holder, final int position) {
        Operationinfo operationinfo = mDatas.get(position);
        if(TextUtils.isEmpty(operationinfo.getNickName()))
        {
            holder.name.setText("用户"+mDatas.get(position).getOrder());
        }else{
            holder.name.setText(operationinfo.getNickName());
        }

        holder.time.setText(TimeUtil.formatForLogging(Long.parseLong(operationinfo.getTime())*1000));

        if(operationinfo.getResult() == OpenResult.FAILED){
            holder.rightbtn.setVisibility(View.VISIBLE);
            holder.rightbtn.setBackgroundColor(MyApplication.context.getResources().getColor(R.color.transparent));
            holder.rightbtn.setTextColor(MyApplication.context.getResources().getColor(R.color.red_normal));
            holder.rightbtn.setText("失败");

        }else{
            holder.rightbtn.setVisibility(View.GONE);
        }

        holder.rootlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.onItemClick(mDatas.get(position), position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }


        View rootlayout;
        ImageView lefticon;
        TextView name;
        TextView time;
        Button rightbtn;
        ImageView righticon;
        QMUILoadingView loadingView;

    }
}
